package idv.loveloser.servlet;

import idv.loveloser.dao.MdlForumDiscussions;
import idv.loveloser.dao.MdlForumDiscussionsDAO;
import idv.loveloser.dao.MdlForumPosts;
import idv.loveloser.dao.MdlForumPostsDAO;
import idv.loveloser.util.Util;

import java.io.IOException;
import java.io.PrintWriter;

import javax.resource.spi.DissociatableManagedConnection;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

public class PostForumContent extends HttpServlet {
	private MdlForumPostsDAO postDAO = new MdlForumPostsDAO();
	private MdlForumDiscussionsDAO discussionDAO = new MdlForumDiscussionsDAO();
	/**
	 * Constructor of the object.
	 */
	public PostForumContent() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		response.setCharacterEncoding("UTF-8");
		String client = request.getParameter("client");
		String sessionKey = request.getParameter("sessionKey");
		String courseId=request.getParameter("courseId");
		MdlForumPosts post = new MdlForumPosts();
		if(Util.isAuthorised(client, sessionKey)){
			String forumId = request.getParameter("forumId");
			String parentId=request.getParameter("parent");
			String message=request.getParameter("message");
			String subject=request.getParameter("subject");
			
			long userId=Util.getUserId(Long.parseLong(client));
			MdlForumDiscussions discussion =new MdlForumDiscussions();
			discussion.setAssessed(true);
			discussion.setCourse(Long.parseLong(courseId));
			discussion.setFirstpost(post.getId());
			discussion.setForum(Long.parseLong(forumId));
			discussion.setGroupid(-1L);
			discussion.setName(subject);
			discussion.setTimeend(0L);
			discussion.setTimemodified(System.currentTimeMillis());
			discussion.setTimestart(0L);
			discussion.setUserid(userId);
			discussion.setUsermodified(userId);
			discussion.setFirstpost(0L);
			discussionDAO.save(discussion);
		
			post.setCreated(System.currentTimeMillis());
			post.setDiscussion(discussion.getId());
			post.setMailed((short)0);
			post.setMailnow((long)0);
			post.setMessage(message);
			post.setMessageformat((short)1);
			post.setMessagetrust((short)0);
			post.setModified(System.currentTimeMillis());
			post.setParent(0L);
			post.setSubject(subject);
			post.setAttachment("");
			post.setTotalscore((short)0);
			post.setUserid(userId);
			postDAO.save(post);
			discussion.setFirstpost(post.getId());
			discussionDAO.merge(discussion);
		}
		Gson gson = new Gson();
		out.println(gson.toJson(post));
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
